package com.xjscrm.console.chat.core.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RNoReadMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkRecord;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.service.customer.chat.ChatTalkRecordService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatNoReadMsgHandler extends ChatBaseMsgHandler<RNoReadMsg> {

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;
    @Autowired
    private ChatTalkRecordService chatTalkRecordService;
    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;

    @Override
    public SResultMsg doHandle(RNoReadMsg content, ChannelHandlerContext ctx) {

        Long merId = content.getMerId();
        Long staffId = content.getStaffId();

        Long talkSessionId = content.getTalkSessionId();
        try {

            ChatTalkSession talkSession = chatTalkSessionService.getById(merId, talkSessionId);
            if (Objects.isNull(talkSession)) {
                throw new BusinessException("未读消息失败: 会话不存在");
            }
            ChatTalkSession update = new ChatTalkSession();
            update.setId(talkSession.getId());
            update.setMerId(talkSession.getMerId());
            //
            ChatTalkRecord talkRecord = chatTalkRecordService.getLastTalk(merId, talkSession.getDeviceUniqueId(), talkSession.getId());
            //
            Integer lastReadTime;
            if (Objects.isNull(talkRecord)) {
                lastReadTime = (int) (System.currentTimeMillis() / 1000L);
            } else {
                lastReadTime = talkRecord.getSendTime();
            }
            update.setLastReadTime(lastReadTime);
            update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
            chatTalkSessionService.update(update);
            return SResultMsg.success(content, "未读回话成功");
        } catch (Exception e) {
            log.error("未读回话失败", e);
            throw new BusinessException("未读回话失败");
        }
    }

    @Override
    public boolean checkMsg(RNoReadMsg baseMsg) {
        return super.checkMsg(baseMsg) || ObjectUtils.isEmpty(baseMsg.getTalkSessionId());
    }

    @Override
    public Class<RNoReadMsg> msgClass() {
        return RNoReadMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_no_read_msg;
    }

}
